﻿using Autofac;
using Common.Infrastructrue;
using System.Reflection;
using Module = Autofac.Module;

namespace RabitPart.ReadApi
{
    public class AutofacModuleRegister : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
             
            // 加载程序集
            var applicationServicesAssembly = Assembly.Load("Common.Infrastructrue");

            // 注册所有以 "Service" 结尾的类型为其实现的接口
            // builder.RegisterAssemblyTypes(applicationServicesAssembly)
            //     .Where(t => t.Name.EndsWith("Service"))
            //     .AsImplementedInterfaces();

            // 注册所有实现了 IBaseRepository 接口的接口
            builder.RegisterAssemblyTypes(applicationServicesAssembly)
                .Where(t => typeof(IBaseResponstity<>).IsAssignableFrom(t) && t.IsInterface)
                .AsSelf(); // 注册接口本身

            // 注册所有继承了 BaseRepository 类的类
            builder.RegisterAssemblyTypes(applicationServicesAssembly)
                .Where(t => typeof(BaseResponstity<>).IsAssignableFrom(t) && !t.IsAbstract)
                .AsSelf(); // 注册类本身
        }
    }
}
